package com.jhmk.jhecis.config.job;


import com.jhmk.jhecis.config.HttpRequestHelper;
import com.jhmk.jhecis.utils.JsonUtils;
import com.jhmk.jhecis.dto.ApiJob;
import jakarta.annotation.Resource;
import org.quartz.*;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;

@Component
public class RestJob implements Job {

    @Resource
    HttpRequestHelper httpRequestHelper;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        String data = (String) jobDataMap.get("data");
        if (data != null) {
            Map<String, String> map=  new HashMap<>();
            map.put("name", "ming");
            System.out.println(JsonUtils.objToStr(map));

            System.out.println(data);
            ApiJob jobData = JsonUtils.strToObj(data, ApiJob.class);
            HttpMethod httpMethod = jobData.getType().toUpperCase() == "POST" ? HttpMethod.POST : HttpMethod.GET;
            String res = "";
            try {
                if (httpMethod == HttpMethod.GET) {
                    Map<String, Object> dt = JsonUtils.strToObj(jobData.getData().toString(), Map.class);
                    if (dt == null) {dt=new HashMap<>();}
                    System.out.println(jobData.getUrl());
                    res = httpRequestHelper.get(jobData.getUrl(), dt);
                } else {
                    res = httpRequestHelper.request(jobData.getUrl(), jobData.getData().toString(), httpMethod);
                }
            } catch (Exception e) {
                System.out.println(e.toString());
            }
            System.out.println("发送请求");
            System.out.println(res);
        }

    }
}
